package swimv2gm.client;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.ExistingDataException;
import swimv2gm.bl.session.UserManager;

/**
 * Inizializzazione del primo account admin. Per motivi di sicurezza, è
 * opportuno rimuovere questa pagina ad installazione ultimata.
 * 
 * @author Matteo Gallesio
 */
public class Init extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		UserManager man;
		try {
			man = (UserManager)InitialContext.doLookup("UserManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		java.io.PrintWriter out = response.getWriter();

		long uid = -1;
		out.println("<!DOCTYPE html><html>");
		try {
			uid = man.createAdmin("admin@swimv2.com","adminpassword");
			out.println("Sistema inizializzato.<br />");
			out.println("ID amministratore: " + uid + "<br />");
			out.println("Email amministratore: admin@swimv2.com<br />");
			out.println("Password amministratore: admin.<br />");
			out.println("<a href=\"/SWIMv2gm/\">Pagina iniziale</a>");
		} catch (ExistingDataException e) {
			out.println("Errore: e-mail inserita già presente");
		}
		out.println("</html>");

	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
